package com.xqt.service.dict;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.xqt.base.dao.BaseDao;
import com.xqt.base.entity.BaseEntity;
import com.xqt.base.page.PageInfo;
import com.xqt.base.service.BaseService;
import com.xqt.dao.dict.DictDao;
import com.xqt.entity.dict.DictBase;
import com.xqt.inters.service.dict.IDictService;
import com.xqt.util.StringUtil;

/**
 * Desc:
 * Author:liubo
 * Date: 2017-03-21 16:00
 **/
public class DictService extends BaseService<DictBase> implements IDictService {

    @Autowired
    private DictDao dictDao;


    @Override
    public void delete(Object... ids) {
        if (ids == null || ids.length < 1) {
            return;
        }
        for (Object id : ids) {
            dictDao.delete(id);
        }
    }


    @Override
    public void updateBySelective(DictBase dictBase) throws Exception {
        preUpdate(dictBase);
        dictDao.updateBySelective(dictBase);
    }

    @Override
    public int queryByCount(BaseEntity page) {
        return dictDao.queryByCount(page);
    }

    @Override
    public List<DictBase> queryByList(BaseEntity page) {
        return dictDao.queryByList(page);
    }


    @Override
    public DictBase getDictLabel(DictBase dictBase) {
        return dictDao.getDictLabel(dictBase);
    }

    @Override
    public DictBase getDictName(DictBase dictBase)  {
        return dictDao.getDictName(dictBase);
    }

    @Override
    public List<DictBase> getDictList(Object dictType) {
        return dictDao.getDictList(dictType);
    }

    @Override
    public PageInfo<DictBase> queryTypeByList(BaseEntity page) throws Exception {
		return getDao().selectPageByEntity(page, "queryTypeByCount", "queryTypeByList");
    }

    @Override
    public void addTypeSave(DictBase dictBase, String oldDictType) throws Exception {
        // 新增保存
        if (StringUtil.isEmpty(oldDictType)) {
            add(dictBase);
        } else {

            List<DictBase> dictList = getDictList(oldDictType);
            // 更新保存
            for (DictBase dict : dictList) {
                dict.setDictType(dictBase.getDictType());
                dict.setDictTypeName(dictBase.getDictTypeName());
                updateBySelective(dict);
            }
        }
    }

    @Override
    public void deleteDictType(DictBase dictBase) throws Exception {
        // 物理删除
        if (dictBase == null || StringUtil.isEmpty(dictBase.getDictType())) {
            return;
        }
        List<DictBase> dictList = getDictList(dictBase.getDictType());
        // 更新保存
        for (DictBase dict : dictList) {
            delete(dict);
        }
    }

    @Override
    public List<DictBase> checkDictExist(DictBase dictBase) {
        return dictDao.checkDictExist(dictBase);
    }

    @Override
    public DictBase queryByType(DictBase dictBase) {
        return dictDao.queryByType(dictBase);
    }

    @Override
    public List<DictBase> getDictLabels(DictBase dictBase) {
        return dictDao.getDictLabels(dictBase);
    }

	@Override
	public BaseDao<DictBase> getDao() {
		return dictDao;
	}
}
